#include "Direct3DFramework.h"

int Direct3DFramework::createTexture(BeanieTexture* a_texture, char* a_filePath)
{
	//check if texture exist
	for(int i = 0; i < m_textureList.size(); i++)
	{
		D3DFTexture* texture = m_textureList.get(i);
		if(a_filePath == texture->m_filePath)
		{
			//set texture info
			a_texture->m_texId		= texture->m_texId;
			a_texture->m_filePath	= texture->m_filePath;
			a_texture->m_width		= texture->m_info.Width;
			a_texture->m_height		= texture->m_info.Height;

			//create and set matrices to identity
			a_texture->m_matId = texture->m_matrices.size();
			texture->m_matrices.insertBack(D3DFMatrices());
			D3DXMatrixIdentity(&texture->m_matrices[a_texture->m_matId].m_scale);
			D3DXMatrixIdentity(&texture->m_matrices[a_texture->m_matId].m_rotate);
			D3DXMatrixIdentity(&texture->m_matrices[a_texture->m_matId].m_translate);
			D3DXMatrixIdentity(&texture->m_matrices[a_texture->m_matId].m_world);

			return GPX_SUCCESS;
		}
	}

	//create texture
	D3DFTexture* texture = new D3DFTexture;
	if(FAILED(D3DXCreateTextureFromFileEx(
		m_device,
		TEXT(a_filePath),
		D3DX_DEFAULT_NONPOW2,
		D3DX_DEFAULT_NONPOW2,
		0,0,
		D3DFMT_UNKNOWN,
		D3DPOOL_MANAGED,
		D3DX_DEFAULT,
		D3DX_DEFAULT,
		D3DCOLOR_XRGB(255,0,255),
		&texture->m_info,0,
		&texture->m_texture)))
		return GPX_LOAD_TEXTURE_ERROR;

	//set texture info
	texture->m_texId		= m_textureList.size();
	strcpy_s(texture->m_filePath,a_filePath);
	a_texture->m_texId		= texture->m_texId;
	a_texture->m_filePath	= texture->m_filePath;
	a_texture->m_width		= texture->m_info.Width;
	a_texture->m_height		= texture->m_info.Height;
	

	//create and set matrices to identity
	a_texture->m_matId  = texture->m_matrices.size();
	texture->m_matrices.insertBack(D3DFMatrices());
	D3DXMatrixIdentity(&texture->m_matrices[a_texture->m_matId].m_scale);
	D3DXMatrixIdentity(&texture->m_matrices[a_texture->m_matId].m_rotate);
	D3DXMatrixIdentity(&texture->m_matrices[a_texture->m_matId].m_translate);
	D3DXMatrixIdentity(&texture->m_matrices[a_texture->m_matId].m_world);

	//add texture to texture list
	m_textureList.insertBack(texture);

	return GPX_SUCCESS;
}